package org.zjx.action.game;

import org.zjx.action.Action;
import org.zjx.analysis.GameClickAnalysis;
import org.zjx.core.IWindow;
import org.zjx.entity.Address;

import java.awt.*;

public class ClickAddressAction extends Action {
//    private GameAnalysis gameAnalysis;
    private GameClickAnalysis gameClickAnalysis;
    private final Address targetAddress;

    private void analysis() {
//        gameAnalysis = new GameAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
        gameClickAnalysis = new GameClickAnalysis(targetWindow, targetWindow.getState().getGameState().getScreenshot(), targetAddress);
    }

    public ClickAddressAction(IWindow targetWindow, Address targetAddress) {
        super(targetWindow, false);
        this.targetAddress = targetAddress;
    }

    @Override
    protected void doExecute() throws Exception {
        new GameAction(targetWindow).execute();
        analysis();
        Point point = gameClickAnalysis.getSafeClickPoint();
        move(point.x, point.y);
        click();
        delay(200);
    }

    public void move(int x, int y) {
        targetWindow.getState().getGameState().setGameMousePoint(new Point(x, y));
        targetWindow.execute(op -> {
            op.gameMove(x, y);
            return null;
        });
    }

    public void click() {
        targetWindow.execute(op -> {
            op.click(1);
            return null;
        });
    }

    public void delay(int milliseconds) {
        targetWindow.execute(op -> {
            op.delay(milliseconds);
            return null;
        });
    }
}
